package com.example.telesys.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.telesys.mapper.SupplierMapper;
import com.example.telesys.pojo.Supplier;
import com.example.telesys.pojo.SupplierExample;
import com.example.telesys.service.SupplierService;
import jdk.nashorn.internal.ir.Block;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;


    /**
     * 模拟Sentinel中熔断降级规则中的异常比例问题
     *
     * @param supplierid
     * @return
     */
    @Override
    @SentinelResource(value = "selectSupplierById")
    public Supplier selectSupplierById(String supplierid) {
        SupplierExample supplierExample = new SupplierExample();
        SupplierExample.Criteria criteria = supplierExample.createCriteria();
        criteria.andSupplieridEqualTo(supplierid);
        return supplierMapper.selectByExample(supplierExample).get(0);
    }


    @Override
    @Transactional
    public int addPhonePriceTotal(Integer phoneTotal, String supplierid) {
        Supplier supplier = selectSupplierById(supplierid);
        supplier.setPhonetotal(supplier.getPhonetotal() + phoneTotal);
        SupplierExample supplierExample = new SupplierExample();
        SupplierExample.Criteria criteria = supplierExample.createCriteria();
        criteria.andSupplieridEqualTo(supplierid);
        return supplierMapper.updateByExampleSelective(supplier, supplierExample);
    }

    @Override
    @Transactional
    public int addBroadPriceTotal(Integer broadbandTotal, String supplierid) {
        Supplier supplier = selectSupplierById(supplierid);
        supplier.setBroadbandtotal(supplier.getPhonetotal() + broadbandTotal);
        SupplierExample supplierExample = new SupplierExample();
        SupplierExample.Criteria criteria = supplierExample.createCriteria();
        criteria.andSupplieridEqualTo(supplierid);
        return supplierMapper.updateByExampleSelective(supplier, supplierExample);
    }

    @Override
    @Transactional
    public int addTrafficPriceTotal(Integer trafficTotal, String supplierid) {
        Supplier supplier = selectSupplierById(supplierid);
        supplier.setTraffictotal(supplier.getTraffictotal() + trafficTotal);
        SupplierExample supplierExample = new SupplierExample();
        SupplierExample.Criteria criteria = supplierExample.createCriteria();
        criteria.andSupplieridEqualTo(supplierid);
        return supplierMapper.updateByExampleSelective(supplier, supplierExample);
    }

    @Override
    public List<Supplier> selectSupplierData() {
        return supplierMapper.selectByExample(null);
    }
}
